from pages.SystemManage.SystemManage import SystemManage
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException
from common.GetLoc import CompanyPageLoc
import time


class CompanyPage(SystemManage):
    loc_citySelector = CompanyPageLoc().get_loc_citySelector()
    loc_companyName = CompanyPageLoc().get_loc_companyName()
    loc_cityName = CompanyPageLoc().get_loc_cityName()

    def __init__(self, browser):
        super().__init__(browser)
        self.login()
        self.find_ele(self.loc_systemManage).click()
        self.find_ele(self.loc_companyList).click()

    @staticmethod
    def loc_concat(loc, text):
        way = loc[0]
        eleValue = loc[1] % text
        print(way, eleValue, sep='\n')
        return [loc[0], loc[1] % text]

    def add_company(self, city_name, company_name):
        self.find_ele(self.loc_addButton).click()
        self.find_ele(self.loc_citySelector).click()
        time.sleep(2)
        self.find_ele(self.loc_concat(self.loc_cityName, city_name)).click()
        self.find_ele(self.loc_companyName).send_keys(company_name)
        self.find_ele(self.loc_confirmButton).click()
        try:
            assert company_name == self.find_ele(self.loc_lastContent).text, '添加失败'
        except AssertionError:
            return False
        else:
            return True

    def edit_company(self, new_city, old_companyName, new_companyName):
        self.chose_checkBox(old_companyName)
        self.find_ele(self.loc_editButton).click()
        self.find_ele(self.loc_concat(self.loc_cityName, new_city)).click()
        self.find_ele(self.loc_companyName).clear()
        self.find_ele(self.loc_companyName).send_keys(new_companyName)
        self.find_ele(self.loc_confirmButton).click()
        try:
            assert new_companyName == self.find_info(new_companyName), '编辑失败'
        except NoSuchElementException and AssertionError:
            return False
        else:
            return True

    def delete_company(self, company_name):
        self.chose_checkBox(company_name)
        self.find_ele(self.loc_delButton).click()
        self.find_ele(self.loc_alertButton).click()
        try:
            self.find_ele(self.loc_checkBox % company_name).click()
        except StaleElementReferenceException:
            return True
        else:
            return False


if __name__ == '__main__':
    a = CompanyPage(webdriver.Chrome())
    a.add_company('杭州', '米哈游')
    # a.edit_company('成都','米哈游','汇智动力')
    # a.delete_company('汇智动力')
    b = input()
